package com.fyt.reachgo.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fyt.reachgo.domain.Result;
import com.fyt.reachgo.domain.TravelRequest;
import com.fyt.reachgo.domain.VO.UserToCityCountVO;
import com.fyt.reachgo.domain.VO.UserToCityVO;
import com.fyt.reachgo.domain.dto.UserDTO;
import com.fyt.reachgo.service.TravelRequestService;
import com.fyt.reachgo.mapper.TravelRequestMapper;
import com.fyt.reachgo.utils.RedisConstants;
import com.fyt.reachgo.utils.UserHolder;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
* @author 27675
* @description 针对表【travel_request(用户行程规划请求记录表)】的数据库操作Service实现
* @createDate 2025-02-25 08:37:04
*/
@Service
@RequiredArgsConstructor
public class TravelRequestServiceImpl extends ServiceImpl<TravelRequestMapper, TravelRequest>
    implements TravelRequestService{

    private final TravelRequestMapper travelRequestMapper;
    private final StringRedisTemplate stringRedisTemplate;

    @Override
    public Result getTravelReqByUser() {
        UserDTO user = getUser();
        if (user == null) {
            return Result.error("用户未登录");
        }
        // 优先查缓存
        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + user.getUserId()))){
            // 缓存存在
            String json = stringRedisTemplate.opsForValue().get(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + user.getUserId());
            // 数据是否为空
            if (StrUtil.isBlank(json)) {
                // 解决缓存穿透
                return Result.error("用户未规划任何行程");
            }
            List<UserToCityVO> userToCityVO = JSONUtil.toList(json, UserToCityVO.class);
            return Result.success(userToCityVO);
        }
        // 缓存不存在
        List<UserToCityVO> userToCityVO = travelRequestMapper.getUserTravelReq(user.getUserId());
        if (userToCityVO.isEmpty()) {
            // 解决缓存穿透
            stringRedisTemplate.opsForValue().set(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + user.getUserId(),
                    "",
                    RedisConstants.CACHE_NULL_TTL,
                    TimeUnit.MINUTES);
            return Result.error("用户未规划任何行程");
        }
        stringRedisTemplate.opsForValue().set(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + user.getUserId(), JSONUtil.toJsonStr(userToCityVO));
        stringRedisTemplate.expire(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + user.getUserId(),
                RedisConstants.GET_TRAVEL_REQ_BY_USER_TTL + RandomUtil.randomLong(0, 15),
                TimeUnit.MINUTES);
        return Result.success(userToCityVO);
    }

    @Override
    public Result getTravelReqByUsers() {
        UserDTO user = getUser();
        if (user == null) {
            return Result.error("用户未登录");
        }
        if (Boolean.TRUE.equals(stringRedisTemplate.hasKey(RedisConstants.GET_TRAVEL_REQ_BY_USERS_KEY))){
            String json = stringRedisTemplate.opsForValue().get(RedisConstants.GET_TRAVEL_REQ_BY_USERS_KEY);
            if (StrUtil.isBlank(json)) {
                return Result.error("操作失败");
            }
            List<UserToCityCountVO> userToCityCountVOS = JSONUtil.toList(json, UserToCityCountVO.class);
            return Result.success(userToCityCountVOS);
        }
        List<UserToCityCountVO> userToCityCountVOS = travelRequestMapper.getUserTravelReqs(user.getUserId());
        if (userToCityCountVOS.isEmpty()) {
            stringRedisTemplate.opsForValue().set(RedisConstants.GET_TRAVEL_REQ_BY_USERS_KEY,
                    "",
                    RedisConstants.CACHE_NULL_TTL,
                    TimeUnit.MINUTES);
            return Result.error("操作失败");
        }
        stringRedisTemplate.opsForValue().set(RedisConstants.GET_TRAVEL_REQ_BY_USERS_KEY,
                JSONUtil.toJsonStr(userToCityCountVOS));
        stringRedisTemplate.expire(RedisConstants.GET_TRAVEL_REQ_BY_USERS_KEY,
                RedisConstants.GET_TRAVEL_REQ_BY_USERS_TTL + RandomUtil.randomLong(0, 15),
                TimeUnit.MINUTES);
        return Result.success(userToCityCountVOS);
    }

    @Override
    public Result getTravelReqByUserId(Long userId) {
        if (userId == null) {
            return Result.error("用户id为空");
        }
        // 优先查缓存
        if(Boolean.TRUE.equals(stringRedisTemplate.hasKey(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + userId))){
            // 缓存存在
            String json = stringRedisTemplate.opsForValue().get(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + userId);
            // 数据是否为空
            if (StrUtil.isBlank(json)) {
                // 解决缓存穿透
                return Result.error("用户未规划任何行程");
            }
            List<UserToCityVO> userToCityVO = JSONUtil.toList(json, UserToCityVO.class);
            return Result.success(userToCityVO);
        }
        // 缓存不存在
        List<UserToCityVO> userToCityVO = travelRequestMapper.getUserTravelReq(userId);
        if (userToCityVO.isEmpty()) {
            // 解决缓存穿透
            stringRedisTemplate.opsForValue().set(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + userId,
                    "",
                    RedisConstants.CACHE_NULL_TTL,
                    TimeUnit.MINUTES);
            return Result.error("用户未规划任何行程");
        }
        stringRedisTemplate.opsForValue().set(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + userId, JSONUtil.toJsonStr(userToCityVO));
        stringRedisTemplate.expire(RedisConstants.GET_TRAVEL_REQ_BY_USER_KEY + userId,
                RedisConstants.GET_TRAVEL_REQ_BY_USER_TTL + RandomUtil.randomLong(0, 15),
                TimeUnit.MINUTES);
        return Result.success(userToCityVO);
    }


    private UserDTO getUser() {
        return UserHolder.getUser();
    }
}




